<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8">
      <el-card shadow="hover">
        <div class="user">
          <img :src="userImg">
          <div class="userInfo">
            <span class="name">admin</span>
            <span class="access">超级管理员</span>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录的时间:<span> 2022-7-19</span></p>
          <p>上次登录的地点:<span> 广州</span></p>
        </div>
      </el-card>
      <el-card style="margin-top:20px">
        <el-table :data="tableData">
          <el-table-column type="selection">
          </el-table-column>
          <el-table-column v-for="(val, index) in tableLabel" :key="index" :prop="index" :label="val"></el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16">
      <div class="num-wrap">
        <el-card class="card" v-for="item in countData" :key="item.name" :body-style="{ display: 'flex', padding: 0 }">
          <i class="icon" :class="`el-icon-${item.icon}`" :style="{ background: item.color }"></i>
          <div class="detail">
            <span class="num">￥{{ item.value }}</span>
            <span class="txt">*{{ item.name }}</span>
          </div>
        </el-card>
      </div>

      <el-card class="graph-top" style="height:220px">
        <div :style="{ height: '220px', width: '600px' }" ref="echarts"></div>
      </el-card>
      <div class="graph-bottom">
        <el-card class="graph" :style="{ marginRight: '10px' }">
          <div :style="{ height: '200px', width: '300px' }" ref="userEcharts"></div>
        </el-card>
        <el-card class="graph">
          <div :style="{ height: '200px', width: '300px' }" ref="videoEcharts"></div>
        </el-card>
      </div>
    </el-col>

  </el-row>
</template>

<script>
import { getData } from '../../api/data';
import * as echarts from 'echarts'
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "home",
  data() {
    return {
      userImg: require('../../src/assets/user.jpg'),
      tableData: [
        {
          name: "oppo",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800
        },
        {
          name: "vivo",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800
        },
        {
          name: "apple",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800
        },
        {
          name: "xiaomi",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800
        },
        {
          name: "sansung",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800
        },
        {
          name: "meizu",
          todayBuy: 100,
          monthBuy: 300,
          totalBuy: 800
        }
      ],
      tableLabel: {
        name: '品牌',
        todayBuy: "今日购买",
        monthBuy: "本月购买",
        totalBuy: "总购买",
      },
      countData: [
        {
          name: "今日支付订单",
          value: 124,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "今日收藏订单",
          value: 13,
          icon: "star-on",
          color: "#ffe980",
        },
        {
          name: "今日未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
        {
          name: "本月支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "今月收藏订单",
          value: 201,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "本月未支付订单",
          value: 223,
          icon: "s-goods",
          color: "#5ab1ef",
        },
      ]
    }
  },
  created() {
    getData().then(res => {
      const { code, data } = res.data;
      if (code === 20000) {
        this.tableData = data.tableData;
        const order = data.orderData;
        const keyArray = Object.keys(order.data[0]);
        const series = [];
        const xData = order.date;
        keyArray.forEach(key => {
          series.push({
            name: key,
            data: order.data.map(item => item[key]),
            type: 'line'
          })
        });
        const option = {
          xAxis: {
            data: xData
          },
          yAxis: {

          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: keyArray
          },
          series
        }
        const E = echarts.init(this.$refs.echarts);
        E.setOption(option);


        // 用户图
        const userOption = {
          legend: {
            data: ['新增用户', '活跃用户'],
            textStyle: {
              color: '#333',
            },
          },
          // grid: {
          //   left: '20%'
          // },
          tooltip: {
            trigger: 'axis'
          },
          xAxis: {
            type: "category",
            data: data.userData.map(item => item.data),
            axisLine: {
              lineStyle: {
                color: "#17b3a3"
              }
            },
            axisLabel: {
              interval: 0,
              color: "#333"
            }
          },
          yAxis: {
            type: "value",
            color: ["#2ec7c9", "#b6a2de"],

          },
          series: [
            {
              name: '新增用户',
              data: data.userData.map(item => item.new),
              type: 'bar',
            },
            {
              name: '活跃用户',
              data: data.userData.map(item => item.active),
              type: 'bar',
            }
          ],
          axisLine: {
            lineStyle: {
              color: "#000"
            }
          },
        };
        const U = echarts.init(this.$refs.userEcharts);
        U.setOption(userOption);

        //饼图
        const videoOption = {
          tooltip: {
            trigger: 'item'
          },
          color: [
            "#0f78f4",
            "#dd536b",
            "#9462e5",
            "#a6a6a6",
            "#e1bb22",
            "#39c362",
            "#3ed1cf",
          ],
          series: [
            {
              data: data.videoData,
              type: 'pie'
            }
          ]
        }
        const V = echarts.init(this.$refs.videoEcharts);
        V.setOption(videoOption);
      }
    });
      // test().then(res => {
      //   console.log(res);
      // })
  },
  computed: {
    // isCollapse() {
    //   return this.$store.state.tab.isCollapse
    // },
  }
}
</script>

<style lang="less" scoped>
.user {
  display: flex;

  img {
    width: 80px;
    height: 80px;
    border-radius: 50%;
  }

  .userInfo {
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin-left: 10px;

    .name {
      font-weight: 600;
      font-size: 25px;
    }

    .access {
      color: #aaa;
      font-size: 14px;
    }
  }
}

.login-info {
  color: #777;
  font-size: 13px;
}

.num-wrap {
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
  justify-content: space-between;

  .card {
    display: flex;
    height: 50px;
    margin-bottom: 10px;
    min-width: 230px;
    max-width: 300px;

    .icon {
      width: 50px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      color: #fff;
    }

    .detail {
      display: flex;
      flex-direction: column;
      margin-left: 5px;

      .num {
        font-size: 18px;
      }

      .txt {
        font-size: 12px;
        color: #aaa;
      }
    }
  }
}

.graph-top {
  margin: 10px 0;
  display: flex;
  justify-content: center;
}

.graph-bottom {
  display: flex;
  flex-direction: row;

  .graph {
    flex: 1;
    display: flex;
    justify-content: center;
  }
}
</style>